export type Id = number

export class Time {
  static readonly BASE = 60

  static fromPrimary(sec: number) {
    const time = new Time()
    time.sec = sec
    return time
  }

  static fromMinSec(min: number, sec: number) {
    const time = new Time()
    time.sec = min * Time.BASE + sec
    return time
  }

  private sec = 0

  getMin() {
    return Math.floor(this.sec / Time.BASE)
  }

  getSec() {
    return this.sec % Time.BASE
  }

  fmtChinese() {
    return `${this.getMin()}分${this.getSec()}秒`
  }

  fmtShort() {
    return `${this.getMin().toString().padStart(2, '0')}:${this.getSec().toString().padStart(2, '0')}`
  }

  toPrimary() {
    return this.sec
  }
}

export interface Command {
  id: Id
  name: string
  desc?: string
}

export class Task {
  time: Time

  constructor(
    public id: Id,
    time = new Time(),
  ) {
    this.time = time
  }
}

export enum MessageType {
  Receive,
  Command,
  Error,
}

export const MessageTags = ['收到', '发送', '错误']

export class Message {
  constructor(
    public readonly type: MessageType,
    public readonly time: Time,
    public readonly body = '',
  ) {
  }

  toHTML(typeClass: string) {
    return `[${this.time.fmtShort()}] <span class="${typeClass}">${MessageTags[this.type]}</span>: ${this.body}`
  }

  toText() {
    return `[${this.time.fmtShort()}] ${MessageTags[this.type]}: ${this.body}`
  }
}

export interface Device {
  id: Id
  name: string
}

export interface Trainee {
  id: Id
  name: string
}
